package com.hjj.springboot.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hjj.springboot.entity.Order;
import com.hjj.springboot.entity.OrderDetail;
import com.hjj.springboot.entity.R;
import com.hjj.springboot.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/orderDetail")
public class AdminOrderDetailController {

    @Autowired
    private IOrderDetailService orderDetailService;

    @Autowired
    private IOrderService orderService;

    @GetMapping("/list/{id}")
    public R list(@PathVariable(value = "id") Integer id) {
        List<OrderDetail> orderDetailList = orderDetailService.list(new QueryWrapper<OrderDetail>().eq("mId",id));
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",orderDetailList);
        return R.ok(map);
    }

    @GetMapping("/pieCharts")
    public R pieCharts() {
//        找到状态不为1的订单
        List<OrderDetail> list = orderDetailService.list();
        List<OrderDetail> orderDetailList = list.stream()
                .filter(orderDetail -> orderService.getById(orderDetail.getMId()).getStatus() != 1)
                .collect(Collectors.toList());

        //使用流API和Map来统计每个商品类别的总销售数量
        Map<String, Integer> categoryToSalesMap = orderDetailList.stream()
                .collect(Collectors.toMap(
                        OrderDetail::getGoodsName, // 商品名称作为键
                        OrderDetail::getGoodsNumber,// 销售数量作为值
                        Integer::sum // 如果有相同的商品名称，则累加销售数量
                ));

        // 将统计结果转换为饼图所需的格式
        List<Map<String, Object>> pieChartData = categoryToSalesMap.entrySet().stream()
                .map(entry -> {
                    Map<String, Object> data = new HashMap<>();
                    data.put("name", entry.getKey()); // 商品名称
                    data.put("value", entry.getValue()); // 销售数量
                    return data;
                })
                .collect(Collectors.toList());

        // 将饼图数据填充到map中
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("pieChartData", pieChartData);

        return R.ok(resultMap);
    }

    @GetMapping("/barCharts")
    public R barCharts() {
        //        找到状态不为1的订单
        List<OrderDetail> list = orderDetailService.list();
        List<OrderDetail> orderDetailList = list.stream()
                .filter(orderDetail -> orderService.getById(orderDetail.getMId()).getStatus() != 1)
                .collect(Collectors.toList());
        // 使用Map来同时存储每个商品的价格总和和数量
        Map<String, BigDecimal> goodsToSumMap = new TreeMap<>();
        Map<String, Integer> goodsToCountMap = new TreeMap<>();
        orderDetailList.forEach(orderDetail -> {
            BigDecimal totalPrice = orderDetail.getGoodsPrice().multiply(BigDecimal.valueOf(orderDetail.getGoodsNumber()));
            goodsToSumMap.merge(orderDetail.getGoodsName(), totalPrice, BigDecimal::add); //当前总收入
            goodsToCountMap.merge(orderDetail.getGoodsName(), orderDetail.getGoodsNumber(), Integer::sum); //当前总销售量
        });
        List<String> names = new ArrayList<>(goodsToSumMap.keySet());
        List<BigDecimal> prices = names.stream().map(goodsToSumMap::get).collect(Collectors.toList());
        List<Integer> counts = names.stream().map(goodsToCountMap::get).collect(Collectors.toList());
        HashMap<String, Object> map = new HashMap<>();
        map.put("names",names);
        map.put("prices",prices);
        map.put("counts",counts);
        return R.ok(map);
    }

    @GetMapping("/pricePieCharts")
    public R pricePieCharts() {
        //        找到状态不为1的订单
        List<OrderDetail> list = orderDetailService.list();
        List<OrderDetail> orderDetailList = list.stream()
                .filter(orderDetail -> orderService.getById(orderDetail.getMId()).getStatus() != 1)
                .collect(Collectors.toList());

        //使用流API和Map来统计每个商品类别的总销售数量
        Map<String, BigDecimal> categoryToSalesMap = orderDetailList.stream()
                .collect(Collectors.toMap(
                        OrderDetail::getGoodsName, // 商品名称作为键
                        orderDetail -> orderDetail.getGoodsPrice().multiply(BigDecimal.valueOf(orderDetail.getGoodsNumber())), // 销售额作为值
                        BigDecimal::add // 如果有相同的商品名称，则累加销售
                ));

        // 将统计结果转换为饼图所需的格式
        List<Map<String, Object>> pricePieChartData = categoryToSalesMap.entrySet().stream()
                .map(entry -> {
                    Map<String, Object> data = new HashMap<>();
                    data.put("name", entry.getKey()); // 商品名称
                    data.put("value", entry.getValue()); // 销售额
                    return data;
                })
                .collect(Collectors.toList());

        HashMap<String, Object> map = new HashMap<>();
        map.put("pieChartData", pricePieChartData);
        return R.ok(map);
    }

    @GetMapping("/goodNum")
    public R goodNum() {
        List<OrderDetail> list = orderDetailService.list();
        List<OrderDetail> orderDetailList = list.stream()
                .filter(orderDetail -> orderService.getById(orderDetail.getMId()).getStatus() != 1)
                .collect(Collectors.toList());
        int goodNum = orderDetailList.stream()
                .mapToInt(OrderDetail::getGoodsNumber)
                .sum();
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodNum",goodNum);
        return R.ok(map);
    }
}
